package org.ohuyo.libra.server.dao.impl;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.ohuyo.commons.query.HibernateDaoSupportEx;
import org.ohuyo.libra.server.dao.AppTicketDao;
import org.ohuyo.libra.server.model.table.AppTicketRecord;
import org.springframework.stereotype.Service;

/**
 * 
 * @author rabbit
 * 
 */
@Service("org.ohuyo.libra.server.appTicketDao")
public class AppTicketDaoImpl extends HibernateDaoSupportEx implements
		AppTicketDao {
	@Override
	@Resource(name = "org.ohuyo.libra.server.sessionFactory")
	public void setSessionFactoryEx(SessionFactory sessionFactory) {
		super.setSessionFactoryEx(sessionFactory);
	}

	public void saveAppTicketRecord(AppTicketRecord atr) {
		this.getHibernateTemplate().save(atr);
	}

	public AppTicketRecord getAppTicketRecord(long appTicket) {
		return (AppTicketRecord) this.getHibernateTemplate().get(
				AppTicketRecord.class, appTicket);
	}

	public int deleteInvalidAppTicketRecord() {
		return this.getHibernateTemplateEx().executeUpdateBySql(
				"delete from t_app_ticket where login_ticket not in "
						+ "(select a.login_ticket from t_login_ticket a)");
	}

	public int deleteAppTicketRecordByLoginTicketAndAppCode(
			final long loginTicket, final String appCode) {
		return this.getHibernateTemplateEx().executeUpdateBySql(
				"delete from t_app_ticket where app_code  = ? and login_ticket = ?",
				appCode, loginTicket);
	}

	public int deleteAppTicketRecordByLoginTicket(long loginTicket) {
		return this.getHibernateTemplateEx().executeUpdateBySql(
				"delete from t_app_ticket where login_ticket = ?", loginTicket);
	}

}
